package cvi.com.reader.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cvi.com.reader.R;
import cvi.com.reader.activity.PictureWorksDetailsAcitivity;
import cvi.com.reader.activity.WorksDetailsAcitivity;
import cvi.com.reader.activity.WorksDetailsAcitivity2;
import cvi.com.reader.bean.GridDaitlsBean;
import cvi.com.reader.bean.HotBean;
import cvi.com.reader.tool.UtilsOnclickListen;

/**
 * Created by 梅涛 on 2020/7/23.
 */

public class GridDailtsAdapter extends BaseAdapter {
    private Context context;
    private List<HotBean.RowsBean> gridViewBeanList;

    public GridDailtsAdapter(Context context, List<HotBean.RowsBean> gridViewBeanList) {
        this.context = context;
        this.gridViewBeanList = gridViewBeanList;
    }

    @Override
    public int getCount() {
        return gridViewBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return gridViewBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder viHolder = null;
        HotBean.RowsBean gridViewBean = gridViewBeanList.get(i);
        if (view == null) {
            view = LayoutInflater.from(context).inflate(
                    R.layout.item_grid_daitls, null);
            viHolder = new ViewHolder(view);
            view.setTag(viHolder);
        } else {
            viHolder = (ViewHolder) view.getTag();
        }

        viHolder.workName.setText(gridViewBean.getMaterial_name());
        viHolder.auther.setText(gridViewBean.getAuthor());
        viHolder.firstFloor.getBackground().setAlpha(178);
        viHolder.secondFloor.getBackground().setAlpha(178);
        viHolder.readItNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UtilsOnclickListen.isFastClick()) {
                    Hawk.put("WorksDetails", gridViewBean);
                    if ("0".equals(gridViewBean.getType())) {
                        Intent intent = new Intent(context, WorksDetailsAcitivity2.class);
                        context.startActivity(intent);
                    } else if ("1".equals(gridViewBean.getType())) {
                        Intent intent = new Intent(context, PictureWorksDetailsAcitivity.class);
                        context.startActivity(intent);
                    } else {
                        Intent intent = new Intent(context, WorksDetailsAcitivity2.class);
                        context.startActivity(intent);
                    }


                }
            }
        });
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.work_name)
        TextView workName;
        @BindView(R.id.auther)
        TextView auther;
        @BindView(R.id.first_floor)
        LinearLayout firstFloor;
        @BindView(R.id.read_it_now)
        TextView readItNow;
        @BindView(R.id.second_floor)
        RelativeLayout secondFloor;
        @BindView(R.id.rl_all)
        RelativeLayout rlAll;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
